package com.xin;

import java.util.HashSet;
import java.util.Set;

// 从数组中发现重复的数字,输出其中一个即可
public class RepeatArray {

    public static void main(String[] args) {
        findRepeatNum(new int[]{1,2,3,2});
    }
    public static int findRepeatNum(int[] nums) {
        int result = -1;
        Set<Integer> set = new HashSet<>();
        for (int i : nums) {
            // 当set.add为false时就是重复的情况了
            if (!set.add(i)) {
                result = i;
                break;
            }
        }
        System.out.println("result is: " + result);
        return result;
    }

    private static Integer findRepeatNum1(int[] arr) {

        Integer result = -1;
        Set set = new HashSet();
        for (int i = 0; i < arr.length; i++) {
            if (!set.add(arr[i])) {
                result = arr[i];
                System.out.println(arr[i]);
                break;
            }
        }
        return result;
    }

}
